//
//  HMMainViewController.swift
//  CZWeibo31
//
//  Created by apple on 16/8/9.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class HMMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // 实例化自定义tabBar
        let tabBar = HMTabBar()
        // 设置代理
        tabBar.hmDelegate = self
        // 只读属性
        //self.tabBar
        // 通过kvc 给只读属性赋值
        setValue(tabBar, forKey: "tabBar")
        // 首页
        addChildViewController(HMHomeViewController(), imgName: "tabbar_home", title: "首页")
        // 消息
        addChildViewController(HMMessageViewController(), imgName: "tabbar_message_center", title: "消息")
        // 发现
        addChildViewController(HMDiscoverViewController(), imgName: "tabbar_discover", title: "发现")
        // 我
        addChildViewController(HMProfileViewController(), imgName: "tabbar_profile", title: "我")

    }
    /**
     添加子控制器
     
     - parameter vc:      控制器
     - parameter imgName: 图片名称
     - parameter title:   title
     */
    func addChildViewController(vc: UIViewController, imgName: String, title: String){
        // 设置tilte
//        vc.navigationItem.title = title
//        vc.tabBarItem.title = title
        vc.title = title
        // 设置title
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: UIControlState.Selected)
        // 设置图片
        vc.tabBarItem.image = UIImage(named: imgName)
        // AlwaysOriginal 图片渲染方式
        vc.tabBarItem.selectedImage = UIImage(named: "\(imgName)_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        // 添加子控制器
        addChildViewController(UINavigationController(rootViewController: vc))
    }
}

extension HMMainViewController: HMTabBarDelegate{
    // 02
    func tabBarButtonClick() {
        // 04
        print("撰写按钮点击啦")
        
        if !HMUserAccountViewModel.sharedTools.isLogin{
            print("请登录")
            return
        }
        
        let composeVc = HMComposeViewController()
        self.presentViewController(UINavigationController(rootViewController: composeVc), animated: true, completion: nil)
    }
}
